package com.woniuxy.principal.service.impl;

import com.woniuxy.commons.entity.AllTypeEnroll;
import com.woniuxy.commons.entity.Categories;
import com.woniuxy.commons.entity.EnrolmentRate;
import com.woniuxy.commons.entity.TypeEnroll;
import com.woniuxy.principal.mapper.EnrolmentRateDao;
import com.woniuxy.principal.service.EnrolmentRateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * @author： 雷鹏
 * Date: 2020/7/16 23:09
 */
@Service
public class EnrolmentRateServiceImpl implements EnrolmentRateService {
    @Autowired
    private EnrolmentRateDao enrolmentRateDao;

    @Override
    public AllTypeEnroll getAllTypeEnroll(String year) {


        List<EnrolmentRate> enrolmentRates = enrolmentRateDao.getAllTypeEnroll(year);
        int length = enrolmentRates.size(); //校区个数

        AllTypeEnroll allTypeEnroll = new AllTypeEnroll();    //各校区所有录取类型人数及所有校区名称

        Categories categories = new Categories().setCategories(new String[length]);//各个校区名称

        TypeEnroll disqualify = new TypeEnroll().setName("未录取").setData(new int[length]);   //各校区没有录取的人数
        TypeEnroll junior = new TypeEnroll().setName("专科").setData(new int[length]);   //各校区专科的人数
        TypeEnroll regular = new TypeEnroll().setName("本科").setData(new int[length]);   //各校区本科的人数
        TypeEnroll key = new TypeEnroll().setName("重点").setData(new int[length]);   //各校区重点的人数

        for (int i = 0; i < length; i++) {
            categories.getCategories()[i] = enrolmentRates.get(i).getSname();
            disqualify.getData()[i] = enrolmentRates.get(i).getDisqualifyNum();
            junior.getData()[i] = enrolmentRates.get(i).getJuniorNum();
            regular.getData()[i] = enrolmentRates.get(i).getRegularNum();
            key.getData()[i] = enrolmentRates.get(i).getKeyNum();

        }

        List<TypeEnroll> allTypeEnrollList = new ArrayList<>(); //各校区所有录取类型人数
        allTypeEnrollList.add(disqualify);
        allTypeEnrollList.add(junior);
        allTypeEnrollList.add(regular);
        allTypeEnrollList.add(key);

        allTypeEnroll.setAllTypeEnroll(allTypeEnrollList);
        allTypeEnroll.setCategories(categories);
        return allTypeEnroll;
    }

    @Override
    public AllTypeEnroll getFiveEnroll(int sid) {

        Calendar date = Calendar.getInstance();
        int year = date.get(Calendar.YEAR);
        int month = date.get(Calendar.MONTH);
        if (month > 9) {
            year -= 5;
        } else {
            year -= 6;
        }

        List<EnrolmentRate> fiveEnroll = enrolmentRateDao.getFiveEnroll(sid, year);

        int length = fiveEnroll.size() + 1; //年份数+1

        AllTypeEnroll fiveTypeEnroll = new AllTypeEnroll();    //校区个年份所有录取类型人数及所有年份

        Categories categories = new Categories().setCategories(new String[length]);//各个年份名称

        TypeEnroll disqualify = new TypeEnroll().setName("未录取").setData(new int[length]);   //校区各个年份没有录取的人数
        TypeEnroll junior = new TypeEnroll().setName("专科").setData(new int[length]);   //校区各个年份专科的人数
        TypeEnroll regular = new TypeEnroll().setName("本科").setData(new int[length]);   //校区各个年份本科的人数
        TypeEnroll key = new TypeEnroll().setName("重点").setData(new int[length]);   //校区各个年份重点的人数


        int disqualifyAll = 0;//未录取总人数
        int juniorAll = 0;//专科总人数
        int regularrAll = 0;//本科总人数
        int keyyAll = 0;//重点总人数

        for (int i = 1; i < length; i++) {
            categories.getCategories()[i] = fiveEnroll.get(i - 1).getEnrolmentYear();
            disqualify.getData()[i] = fiveEnroll.get(i - 1).getDisqualifyNum();
            junior.getData()[i] = fiveEnroll.get(i - 1).getJuniorNum();
            regular.getData()[i] = fiveEnroll.get(i - 1).getRegularNum();
            key.getData()[i] = fiveEnroll.get(i - 1).getKeyNum();

            disqualifyAll += fiveEnroll.get(i - 1).getDisqualifyNum();
            juniorAll += fiveEnroll.get(i - 1).getJuniorNum();
            regularrAll += fiveEnroll.get(i - 1).getRegularNum();
            keyyAll += fiveEnroll.get(i - 1).getKeyNum();
        }

        int allNum = 3876;   //高三年级总人数
        int allenroll = disqualifyAll + juniorAll + regularrAll + keyyAll;      //近五年校区高考总人数

        //预计高三年级的升学人数
        categories.getCategories()[0] = "2020预计";
        disqualify.getData()[0] = Math.round(1.0f * disqualifyAll / allenroll * allNum);
        junior.getData()[0] = Math.round(1.0f * juniorAll / allenroll * allNum);
        regular.getData()[0] = Math.round(1.0f * regularrAll / allenroll * allNum);
        key.getData()[0] = Math.round(1.0f * keyyAll / allenroll * allNum);

        List<TypeEnroll> allTypeEnrollList = new ArrayList<>(); //校区各个年份所有录取类型人数
        allTypeEnrollList.add(disqualify);
        allTypeEnrollList.add(junior);
        allTypeEnrollList.add(regular);
        allTypeEnrollList.add(key);

        fiveTypeEnroll.setAllTypeEnroll(allTypeEnrollList);
        fiveTypeEnroll.setCategories(categories);

        return fiveTypeEnroll;
    }


}
